package com.qeeyou.apps.android.craftsman.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.po.InstallTypeInfo;

import java.util.List;

/**
 * Created by lzx on 2018/2/1.
 */

public class InstallTypeGridAdapter extends RecyclerView.Adapter<InstallTypeGridAdapter.ViewHolder> {

    private Context mContext;
    private List<InstallTypeInfo> mList;

    private int mSelectPosition = 0;

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setSelectPosition(int selectPosition) {
        this.mSelectPosition = selectPosition;
        notifyDataSetChanged();
    }

    public InstallTypeGridAdapter(Context context, List<InstallTypeInfo> list) {
        this.mContext = context;
        this.mList = list;
    }

    public void setItems(List<InstallTypeInfo> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_install_type_grid, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        String titleStr = ((InstallTypeInfo)mList.get(position)).getTypename();
        if (!TextUtils.isEmpty(titleStr)) {
            holder.mAmountTv.setText(titleStr);

            if (mSelectPosition == position) {
                holder.mAmountLl.setBackgroundResource(R.drawable.bg_install_type_button_on);
                holder.mAmountTv.setTextColor(mContext.getResources().getColor(R.color.colorBlue));
            } else {
                holder.mAmountLl.setBackgroundResource(R.drawable.bg_install_type_button_off);
                holder.mAmountTv.setTextColor(mContext.getResources().getColor(R.color.colorGray));
            }

            holder.mAmountLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mAmountLl;
        TextView mAmountTv;

        public ViewHolder(View view) {
            super(view);
            mAmountLl = (LinearLayout) view.findViewById(R.id.ll_adapter_install_type_grid);
            mAmountTv = (TextView) view.findViewById(R.id.tv_adapter_install_type_grid);
        }
    }
}
